using System.Collections.Generic;
using UnityEngine;

[SelectionBase]
public class FallingObject : MonoBehaviour
{
	public enum FallingObjectType
	{
		InanimateObject,
		People,
		Vehicle,
		Building,
		Animal
	}

	[SerializeField]
	[Range(1f, 20f)]
	private int _sizeRequired = 1;

	[SerializeField]
	[Range(1f, 20f)]
	private int _botSizeRequired = 1;

	[SerializeField]
	[Range(1f, 20f)]
	private int _points = 1;

	[SerializeField]
	private bool _canBeTransparent;

	[SerializeField]
	private FallingObjectType _objectType;

	[SerializeField]
	private string[] _objectTags;

	[SerializeField]
	private bool _pooledObject;

	private List<IFallingStateListener> _fallingStateListeners;

	private bool _isFalling;

	public int SizeRequired
	{
		get
		{
			return _sizeRequired;
		}
		set
		{
			_sizeRequired = value;
		}
	}

	public int BotSizeRequired
	{
		get
		{
			return _botSizeRequired;
		}
		set
		{
			_botSizeRequired = value;
		}
	}

	public int Points
	{
		get
		{
			return _points;
		}
		set
		{
			_points = value;
		}
	}

	public bool CanBeTransparent => _canBeTransparent;

	public FallingObjectType ObjectType => _objectType;

	public string[] ObjectTags => _objectTags;

	public bool PooledObject
	{
		get
		{
			return _pooledObject;
		}
		set
		{
			_pooledObject = value;
		}
	}

	public bool HasObjectTag(string tag)
	{
		string[] objectTags = _objectTags;
		foreach (string a in objectTags)
		{
			if (a == tag)
			{
				return true;
			}
		}
		return false;
	}

	private void Start()
	{
		Collider[] componentsInChildren = GetComponentsInChildren<Collider>();
		Collider[] array = componentsInChildren;
		foreach (Collider collider in array)
		{
			collider.gameObject.tag = "FallingObject";
			collider.gameObject.layer = LayerMask.NameToLayer("OnGround");
		}
	}

	private void OnEnable()
	{
		if ((bool)BaseGameManager.Instance)
		{
			BaseGameManager.Instance.RegisterFallingObject(this);
		}
	}

	private void OnDisable()
	{
		if (!BaseGameManager.Instance || !BaseGameManager.Instance._hasGameEnded)
		{
			if ((bool)BaseGameManager.Instance)
			{
				BaseGameManager.Instance.UnregisterFallingObject(this);
			}
			if (_pooledObject)
			{
				FallingObjectBehaviour component = GetComponent<FallingObjectBehaviour>();
				UnityEngine.Object.Destroy(component);
			}
		}
	}

	public void RegisterFallingStateListener(IFallingStateListener listener)
	{
		if (_fallingStateListeners == null)
		{
			_fallingStateListeners = new List<IFallingStateListener>();
		}
		_fallingStateListeners.Add(listener);
	}

	public void UnregisterFallingStateListener(IFallingStateListener listener)
	{
		if (_fallingStateListeners != null)
		{
			_fallingStateListeners.Remove(listener);
		}
	}

	public void RaiseFallingStateChanged(bool state)
	{
		if (_fallingStateListeners != null && _isFalling != state)
		{
			_isFalling = state;
			foreach (IFallingStateListener fallingStateListener in _fallingStateListeners)
			{
				fallingStateListener.OnFallingStateChanged(state);
			}
		}
	}
}
